package model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class report {
	public enum ReportType { GroupByCategory, GroupByStoreman, GroupByDate, Unknown, BySurname, ByPeriod }
	private ArrayList<String> report = new ArrayList<String>();
	private ArrayList<storage> repData =  new ArrayList<storage>();
	private ReportType reportType;

	/**
	 * Get the report in string view.
	 *
	 */
	public ArrayList<String> getReport() {
		return report;
	}

	/**
	 * Get the report date for generation report.
	 *
	 */
	public ArrayList<storage> getRepData() {
		return repData;
	}

	/**
	 * Set the report data 
	 *
	 * @param  	repData - The report data
	 * 			repType - The report type of data 
	 */
	public void setRepData(ArrayList<storage> repData, final ReportType repType) {
		reportType = repType;
		if (repType == ReportType.GroupByCategory || repType == ReportType.GroupByStoreman || repType == ReportType.GroupByDate)
		{
			Collections.sort(repData, new Comparator<storage>() {
				@Override
				public int compare(storage o1, storage o2) {
					switch(repType) {
						case GroupByCategory: 
							return o1.getCategory().toString().compareTo(o2.getCategory().toString());
						case GroupByStoreman:
							return o1.getStoreman().compareTo(o2.getStoreman());
						case GroupByDate:
							return o1.getFrom().compareTo(o2.getFrom());
						default: return o1.getStoreman().compareTo(o2.getStoreman()) ;
					}						
				}
			});
		}
		this.repData = repData;
	}

	/**
	 * Generate the full report 
	 *
	 */
	public void generateFullReport() {
		report.clear();
		for(storage st : repData)
		{
			report.add(st.toString());
		}
	}
	
	/**
	 * Generate the short report 
	 *
	 */
	public void generateShortReport() {
		report.clear();
		String groupName;
		Map<String, Integer> map = new HashMap<String, Integer>();

		switch(reportType) {
			case GroupByCategory:{			
				for(int i = 0; i< repData.size();)
				{
					groupName = repData.get(i).getCategory().toString();
					map.put(groupName, 0);
					
					for(; (i < repData.size()) && (repData.get(i).getCategory().toString() == groupName); i++)
						map.put(groupName, map.get(groupName)+1);
					report.add(groupName + ": " + map.get(groupName));
				}
			}
			break;
			case GroupByStoreman:{
				for(int i = 0; i< repData.size(); )
				{
					groupName = repData.get(i).getStoreman();
					map.put(groupName, 0);
			
					for(; (i < repData.size()) && (repData.get(i).getStoreman() == groupName); i++)
						map.put(groupName, map.get(groupName)+1);
					report.add(groupName + ": " + map.get(groupName));
				}
			}
			break;				
			case GroupByDate:{
				for(int i = 0; i< repData.size(); )
				{
					groupName = repData.get(i).getFrom().toString();
					map.put(groupName, 0);
			
					for(; (i < repData.size()) && (repData.get(i).getFrom().toString() == groupName); i++)
						map.put(groupName, map.get(groupName)+1);
					report.add(groupName + ": " + map.get(groupName));
				}
			}
			break;
			
			default: 
				generateFullReport();
				break;
				
		}
	}
	
}
